//
//  SLParserUtil.m
//  SLVideoParser
//
//  Created by S_LINE on 2020/6/19.
//  Copyright © 2020 S_LINE. All rights reserved.
//

#import "SLParserUtil.h"

#import "NSData+SwitchData.h"
#import "NSString+SwitchData.h"

@implementation SLParserUtil

+ (uint64_t)hexToDecimalWithData:(NSData *)sizeData
{
    
    NSString *hexString = [sizeData convertDataToHexStr];
    NSString *integerString = [hexString hexToDecimal];
    NSDecimalNumber *number = [NSDecimalNumber decimalNumberWithString:integerString];
    if ([number isKindOfClass:[NSNumber class]])
    {
        return [number unsignedLongLongValue];
    }
    
    uint64_t value = 0;
    value = *(int*)([sizeData bytes]);
    if (sizeData.length == 1)
    {
        value = *(int*)([sizeData bytes]);
    }
    else if (sizeData.length == 2)
    {
        value = CFSwapInt16BigToHost(*(int*)([sizeData bytes]));
    }
    else if (sizeData.length <= 4)
    {
        value = CFSwapInt32BigToHost(*(int*)([sizeData bytes]));
    }
    else if (sizeData.length <= 8)
    {
        value = CFSwapInt64BigToHost(*(int*)([sizeData bytes]));
    }
    return value;
}

+ (NSString *)hexToStringTypeWithData:(NSData *)typeData
{
    NSString *typeString = [[NSString alloc] initWithData:typeData encoding:NSUTF8StringEncoding];
    return typeString;
}

@end
